package com.liuzh.kaoyanspringboot.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuzh.kaoyanspringboot.common.Constants;
import com.liuzh.kaoyanspringboot.common.Result;
import com.liuzh.kaoyanspringboot.config.AuthAccess;
import com.liuzh.kaoyanspringboot.entity.Article;
import com.liuzh.kaoyanspringboot.entity.Collect;
import com.liuzh.kaoyanspringboot.entity.CollectArticle;
import com.liuzh.kaoyanspringboot.service.ArticleService;
import com.liuzh.kaoyanspringboot.service.CollectService;
import com.liuzh.kaoyanspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.beans.Transient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户文章收藏关联表 前端控制器
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;
    @Autowired
    private ArticleService articleService;

    /**
     * 给收藏夹添加多个文件
     * @param collectId
     * @param articleIds
     * @return
     */
    @PostMapping("/collectArticles/{collectId}")
    public Result collectALot(@PathVariable Integer collectId, @RequestBody List<Integer> articleIds) {
        collectService.setCollectArticles(collectId,articleIds);
        return Result.success();
    }

    /**
     * 将一个文件添加到多个收藏夹下
     * @param articleId
     * @param collectIds
     * @return
     */
    @PostMapping("/aC/{articleId}")
    public Result aToCollectLot(@PathVariable Integer articleId, @RequestBody List<Integer> collectIds) {
        return Result.success(collectIds,collectService.setArticleCollects(articleId,collectIds));
    }

    @PostMapping("/collectArticle")
    public Result collectOne(@RequestBody CollectArticle collectArticle) {
        collectService.setCollectArticle(collectArticle);
        return Result.success();
    }

    /**
     * 获取文章详细信息
     * @param collectId
     * @return
     */

    @GetMapping("/articles/{collectId}")
    public Result getArticlesDetail(@PathVariable Integer collectId,
                                    @RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String name) {
        return Result.success(collectService.getArticlesDetailByCollectId(pageNum, pageSize,collectId,name));
    }

    /**
     * 获取文章id
     * @param collectId
     * @return
     */
    @GetMapping("/collectArticles/{collectId}")
    public Result getCollectArticle(@PathVariable Integer collectId) {
        return Result.success(collectService.getCollectArticle(collectId));
    }

    @GetMapping("/articleInC/{articleId}")
    public Result getArticleInmyColllect(@PathVariable Integer articleId) {
        return Result.success(collectService.getAInCoList(articleId));
    }


    // 新增或更新
    @Transient
    @PostMapping("/new")
    public Result newCollect(@RequestParam(defaultValue = "")  String collectName,
                             @RequestParam Integer articleId) {
        System.out.println("niasidioadi      =====================>");
        Collect collect = new Collect();
        collect.setCollectName(collectName);
        collect.setUserId(TokenUtils.getCurrentUser().getId());
        collect.setCreateTime(DateUtil.now());
        collectService.save(collect);
        System.out.println(collect.getId());
        CollectArticle collectArticle = new CollectArticle();
        collectArticle.setCollectId(collect.getId());
        collectArticle.setArticleId(articleId);
        boolean isCollect = collectService.isCollect(articleId);
        if (!isCollect) {
            articleService.increaseCollect(articleId);
        }
        return Result.success(collectService.setCollectArticle(collectArticle));
    }
    // 新增或更新
    @PostMapping
    public Result save(@RequestBody Collect collect) {
        if (collect.getId() == null) {
            collect.setUserId(TokenUtils.getCurrentUser().getId());
            collect.setCreateTime(DateUtil.now());
        }
        return Result.success(collectService.saveOrUpdate(collect));
    }

    // 根据 id 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(collectService.deleteByCollectId(id));
    }

    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(collectService.removeBatchByIds(ids));
    }

    //查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(collectService.list());
    }

    //查询所有数据
    @GetMapping("/user")
    public Result findUserCollect() {
        return Result.success(collectService.listByUser());
    }

    // 根据 id 查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(collectService.getById(id));
    }

    /**
     * 根据用户id 和 收藏夹名称 获取收藏夹完整内容
     * @param userId
     * @param collectName
     * @return
     */
    @AuthAccess
    @GetMapping("/article")
    public Result findUseCollect(@RequestParam(defaultValue = "0") Integer userId,
                                 @RequestParam(defaultValue = "") String collectName) {
        System.out.println(userId + "==============/n");
        System.out.println(collectName + "===================/n");
        if (userId == 0) {
            return Result.error(Constants.CODE_401,"参数错误");
        }
        return Result.success(collectService.getCollect(userId, collectName));
    }
    //简单的分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String collectName,
                           @RequestParam(required = false,defaultValue = "") String username) {
        Page<Collect> page = collectService.findPage(new Page<>(pageNum, pageSize), collectName, username);
        return Result.success(page);
    }

    @GetMapping("/myPage")
    public Result findMyPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String collectName) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",TokenUtils.getCurrentUser().getId());
        queryWrapper.like("collect_name", collectName);
        return Result.success(collectService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @GetMapping("/isUserCollect/{articleId}")
    public Result isCollect(@PathVariable Integer articleId) {
        return Result.success(collectService.isCollect(articleId));
    }

    /**
     * 找出用户的所有收藏
     * @return
     */
    @GetMapping("/myCollect")
    public Result findMyCollect() {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",TokenUtils.getCurrentUser().getId());
        return Result.success(collectService.list(queryWrapper));
    }

}

